﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Framework.Utility;
using CYTS.Aoyou.Framework.Config;
using CYTS.Aoyou.Framework.Cache;
using CYTS.Aoyou.Framework.CAInterface;

namespace CYTS.Aoyou.Framework.Session
{
    public class SessionHelper
    {
        public static void InitSession()
        {
            if (string.IsNullOrEmpty(GetSessionID()))
            {
                SessionConfig sessionConfig = ConfigFactory.GetConfig<SessionConfig>();
                string sessionID = Guid.NewGuid().ToString();
                CookieHelper.SetCookie(sessionConfig.SessionName, "SessionID", sessionID, DateTime.Now.AddMinutes(60 * 24 * 3));
            }
        }

        public static T GetSession<T>(string key)
        {
            SessionConfig sessionConfig = ConfigFactory.GetConfig<SessionConfig>();
            string sessionID = CookieHelper.GetCookie(sessionConfig.SessionName, "SessionID");
            string realKey = sessionID + key;
            TimeSpan expireTime = DateTime.Now.AddSeconds(sessionConfig.SessionExpireTime) - DateTime.Now;

            var cacheFactory = CacheAdapterFactory.GetInstance();
            ISessionCacheAdapter adapter = cacheFactory.GetCacheAdapter<ISessionCacheAdapter>();
            ISessionBackupCacheAdapter backupAdapter = cacheFactory.GetCacheAdapter<ISessionBackupCacheAdapter>();
            T value = default(T);
            try
            {
                value = adapter.Get<T>(realKey);
                if (value != null)
                {
                    adapter.Set(realKey, value, expireTime);
                }
            }
            catch
            {
                value = backupAdapter.Get<T>(realKey);
                if (value != null)
                {
                    backupAdapter.Set(realKey, value, expireTime);
                }
            }

            return value;
        }

        public static void SetSession<T>(string key, T value)
        {
            SessionConfig sessionConfig = ConfigFactory.GetConfig<SessionConfig>();
            string sessionID = CookieHelper.GetCookie(sessionConfig.SessionName, "SessionID");
            string realKey = sessionID + key;

            TimeSpan expireTime = DateTime.Now.AddSeconds(sessionConfig.SessionExpireTime) - DateTime.Now;
            try
            {
                ISessionCacheAdapter adapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<ISessionCacheAdapter>();
                adapter.Set(realKey, value, expireTime);
            }
            catch 
            {
                ISessionBackupCacheAdapter backupAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<ISessionBackupCacheAdapter>();
                backupAdapter.Set(realKey, value, expireTime);
            }
        }

        public static void RemoveSession(string key)
        {
            SessionConfig sessionConfig = ConfigFactory.GetConfig<SessionConfig>();
            string sessionID = CookieHelper.GetCookie(sessionConfig.SessionName, "SessionID");
            string realKey = sessionID + key;

            try
            {
                ISessionCacheAdapter adapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<ISessionCacheAdapter>();
                adapter.Remove(realKey);
            }
            catch 
            {
                ISessionBackupCacheAdapter backupAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<ISessionBackupCacheAdapter>();
                backupAdapter.Remove(realKey);
            }
        }


        private static string GetSessionID()
        {
            string sessionID = string.Empty;
            SessionConfig sessionConfig = ConfigFactory.GetConfig<SessionConfig>();
            if (sessionConfig != null)
            {
                sessionID = CookieHelper.GetCookie(sessionConfig.SessionName, "SessionID");
            }

            return sessionID;
        }
    }
}
